#include <stdio.h>
#include <stdlib.h>
int main()
{
    int rows = 3, cols = 3;
    int **matrix;

    // 分配内存给行指针
    matrix = (int **)malloc(rows * sizeof(int *));
    if (matrix == NULL)
    {
        printf("内存分配失败\n");
        return 1;
    }

    for (int i = 0; i < rows; i++)
    {
        matrix[i] = (int *)malloc(cols * sizeof(int));
        if (matrix[i] == NULL)
        {
            printf("内存分配失败\n");
            return 1;
        }
    }

    // 初始化并打印矩阵
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            matrix[i][j] = i * cols + j;
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    // 释放内存
    for (int i = 0; i < rows; i++)
    {
        free(matrix[i]); // 释放每一行的内存
    }
    free(matrix); // 释放行指针数组的内存

    return 0;
}